package com.riven.bilibilidemo.activity;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.riven.bilibilidemo.R;
import com.riven.bilibilidemo.adapter.MyRecyclerViewAdapter;
import com.riven.bilibilidemo.fragment.LiveFragment;
import com.riven.bilibilidemo.interfaces.LiveServer;
import com.riven.bilibilidemo.utils.UIUtils;

import butterknife.BindView;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class LiveAppIndexActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {

    Toolbar toolbar;
//    RecyclerView mRecyclerView;

    SwipeRefreshLayout mSwipeRefreshLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_live_app_index);
        toolbar = (Toolbar) findViewById(R.id.live_toolbar);
        FrameLayout frameLayout = (FrameLayout) findViewById(R.id.live_FrameLayout);
        getSupportFragmentManager().beginTransaction().add(R.id.live_FrameLayout,new LiveFragment()).commit();
//        mRecyclerView = (RecyclerView) findViewById(R.id.live_recycle);
        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_live_layout);
        initToolBar();
        initRefreshLayout();
        initData();

    }

    private void initData() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://live.bilibili.com/")
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        LiveServer liveServer = retrofit.create(LiveServer.class);
        liveServer.getLiveAppIndex()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(liveAppIndexInfo ->{
//                    UIUtils.getToast(liveAppIndexInfo.toString());
                    Log.d("zzz", "initData: "+liveAppIndexInfo.toString());
//                    mRecyclerView.setLayoutManager(new LinearLayoutManager(LiveAppIndexActivity.this));
//                    mRecyclerView.setAdapter(new MyRecyclerViewAdapter(LiveAppIndexActivity.this,liveAppIndexInfo));
                    //aaa
                });

    }

    private void initToolBar() {
        toolbar.setTitle("直播");
        setSupportActionBar(toolbar);
        ActionBar supportActionBar = getSupportActionBar();
        if (supportActionBar != null) {
            supportActionBar.setDisplayHomeAsUpEnabled(true);
        }
    }
    public void initRefreshLayout() {
        mSwipeRefreshLayout.setColorSchemeResources(R.color.colorAccent);
        mSwipeRefreshLayout.setOnRefreshListener(this);
    }

    @Override
    public void onRefresh() {
        mSwipeRefreshLayout.setRefreshing(false);
    }
}
